﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Winsteps.Validator;

public partial class Manage_newsAdd : System.Web.UI.Page
{
    BLL.NewsBll nb = new BLL.NewsBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadValidator();

            BindSelect();

           // Response.Write(Request.QueryString["id"].);

            if (Request.QueryString["id"] != null)
            {
                BindNews();
            } 
        }
    }

    private void BindNews()
    {
        int id = int.Parse(Request.QueryString["id"]);
        Model.News news = nb.GetNewsContent(id);

        //Response.Write(news.Title);
        ddl_Category.SelectedIndex = news.Id;
        txt_Title.Text = news.Title;
        txt_Content.Text = news.Content;
        btn_Submit.Text = "修改";
    }

    private void BindSelect()
    {
        DataTable table = nb.GetNewsCategory();
        ddl_Category.DataSource = table;
        ddl_Category.DataBind();
    }

    private void LoadValidator()
    {
        WinVal WinValInstance = new WinVal();
        WinValInstance.SetValidator();

        txt_Title.Attributes.Add("onblur", "this.className='inputblur'");
        txt_Title.Attributes.Add("onfocus", "this.className='inputfoucs'");
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        Model.News news = new Model.News();

        int categoryId = int.Parse(ddl_Category.SelectedValue.ToString());
        string title = txt_Title.Text.ToString();
        string content = Request.Params["editorValue"].ToString();

        if (Request.QueryString["id"] == null)
        {
            string time = DateTime.Now.ToString();

            //此处先定死，以后加入了身份验证再修改
            string author = "Admin";

            news.Author = author;
            news.CategoryId = categoryId;
            news.Content = content;
            news.Time = time;
            news.Title = title;

            if (nb.AddNews(news))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('添加新闻成功');window.location.href=window.location.href;", true);

            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('添加新闻失败');window.location.href=window.location.href;", true);
            }
        }
        else
        {
            int id = int.Parse(Request.QueryString["id"]);
            news.Id = id;
            news.CategoryId = categoryId;
            news.Title = title;
            news.Content = content;

            if (nb.UpdateNews(news))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('修改新闻成功');window.location.href=window.location.href;", true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('修改新闻失败');window.location.href=window.location.href;", true);
            }
        }
    }
}
